Socket
Socket
Sign inDemoInstall

web3-core-subscriptions

Package Overview
Dependencies
4
Maintainers
4
Versions
137
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

web3-core-subscriptions


Version published
Maintainers
4
Install size
4.62 MB
Created

Package description

What is web3-core-subscriptions?

The web3-core-subscriptions package is part of the Web3.js library, which is used to interact with the Ethereum blockchain. This specific package provides functionality for subscribing to various events and data streams from the Ethereum network, such as new blocks, pending transactions, and logs.

What are web3-core-subscriptions's main functionalities?

Subscribe to New Blocks

This feature allows you to subscribe to new block headers on the Ethereum blockchain. The provided code sample demonstrates how to set up a subscription to new block headers using Web3.js and how to handle the incoming data.

const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');

const subscription = web3.eth.subscribe('newBlockHeaders', (error, result) => {
  if (!error) {
    console.log(result);
    return;
  }
  console.error(error);
});

// To unsubscribe
subscription.unsubscribe((error, success) => {
  if (success) {
    console.log('Successfully unsubscribed!');
  }
});

Subscribe to Pending Transactions

This feature allows you to subscribe to pending transactions on the Ethereum network. The code sample shows how to set up a subscription to pending transactions and handle the incoming transaction hashes.

const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');

const subscription = web3.eth.subscribe('pendingTransactions', (error, result) => {
  if (!error) {
    console.log(result);
    return;
  }
  console.error(error);
});

// To unsubscribe
subscription.unsubscribe((error, success) => {
  if (success) {
    console.log('Successfully unsubscribed!');
  }
});

Subscribe to Logs

This feature allows you to subscribe to logs (events) from smart contracts on the Ethereum blockchain. The code sample demonstrates how to set up a subscription to logs with specific options such as contract address and topics.

const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');

const options = {
  address: '0x123456...', // Address of the contract
  topics: ['0x123456...'] // Topics to subscribe to
};

const subscription = web3.eth.subscribe('logs', options, (error, result) => {
  if (!error) {
    console.log(result);
    return;
  }
  console.error(error);
});

// To unsubscribe
subscription.unsubscribe((error, success) => {
  if (success) {
    console.log('Successfully unsubscribed!');
  }
});

Other packages similar to web3-core-subscriptions

Readme

Source

web3-core-subscriptions

NPM Package

This is a sub-package of web3.js

This subscriptions package is used within some web3.js packages.

Please read the documentation for more.

Installation

You can install the package either using NPM or using Yarn

Using NPM

npm install web3-core-subscriptions

Using Yarn

yarn add web3-core-subscriptions

Usage

const Web3Subscriptions = require('web3-core-subscriptions');

const sub = new Web3Subscriptions({
    name: 'subscribe',
    type: 'eth',
    subscriptions: {
        'newBlockHeaders': {
            subscriptionName: 'newHeads',
            params: 0,
            outputFormatter: formatters.outputBlockFormatter
        },
        'pendingTransactions': {
            params: 0,
            outputFormatter: formatters.outputTransactionFormatter
        }
    }
});
sub.attachToObject(myCoolLib);

myCoolLib.subscribe('newBlockHeaders', function(){ ... });

FAQs

Last updated on 05 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc